package com.eairlv.route.server.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eairlv.route.server.entity.Result;
import com.eairlv.route.server.entity.form.RouteForm;
import com.eairlv.route.server.entity.po.RoutePO;
import com.eairlv.route.server.entity.vo.AuthenticationProperties;
import com.eairlv.route.server.mapper.RouteMapper;
import com.eairlv.route.server.utils.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author eairlv
 * @description
 * @date 16:02 2019/12/11
 */
@Slf4j
@Service
public class RouteService {

    public static final String ROUTE_KEY = "paas-route-server:ROUTE:";

    @Autowired
    RouteMapper routeMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AuthenticationService authenticationService;

    /**
     * 通过路由key查询路由信息
     * @param key
     * @return
     */
    public Result<AuthenticationProperties> key(String key, Integer type) {
        String session = (String) redisTemplate.opsForValue().get(redisKey(key, type));
        if (session == null){
            RoutePO routePO = getRoteByKeyAndType(key, type);
            if (routePO != null){
                redisTemplate.opsForValue().set(redisKey(key, type), routePO.getSession());
                session = routePO.getSession();
            }
        }
        String json = (String) redisTemplate.opsForValue().get(AuthenticationService.SESSION_KEY + session);
        AuthenticationProperties authenticationProperties = JSON.parseObject(json, AuthenticationProperties.class);
        return authenticationProperties != null ? Result.ok(authenticationProperties):Result.ok();
    }

    /**
     * 新增或覆盖路由KEY的认证信息
     * @param routeForm
     * @return
     */
    public Result route(RouteForm routeForm) {
        RoutePO routePO = getRoteByKeyAndType(routeForm.getKey(), routeForm.getType());
        if (routePO != null){
            BeanUtil.copyProperties(routeForm, routePO);
            routePO.setRoute_key(routeForm.getKey());
            routeMapper.updateById(routePO);
        } else {
            RoutePO newRoutePO = BeanUtil.copyProperties(routeForm, RoutePO.class);
            newRoutePO.setRoute_key(routeForm.getKey());
            routeMapper.insert(newRoutePO);
        }
        redisTemplate.opsForValue().set(redisKey(routeForm.getKey(), routeForm.getType()), routeForm.getSession());
        return Result.ok();
    }

    /**
     * 通过路由key和类型查询路由对象
     * @param key
     * @param type
     * @return
     */
    private RoutePO getRoteByKeyAndType(String key, Integer type){
        QueryWrapper<RoutePO> queryWrapper = new QueryWrapper<RoutePO>().eq("route_key", key).eq("type", type);
        return routeMapper.selectOne(queryWrapper);
    }

    /**
     * 通过路由key和类型构建redis的键
     * @param key
     * @param type
     * @return
     */
    private String redisKey(String key, Integer type){
        return ROUTE_KEY + key + "#" + type;
    }

    /**
     * 通过路由session获取路由对象
     * @param session
     * @return
     */
    public Result<AuthenticationProperties> session(String session) {
        String json = (String) redisTemplate.opsForValue().get(AuthenticationService.SESSION_KEY + session);
        AuthenticationProperties authenticationProperties = JSON.parseObject(json, AuthenticationProperties.class);
        return authenticationProperties != null ? Result.ok(authenticationProperties):Result.ok();
    }
}
